<template>
  <el-container class="release_container">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="物品名称">
        <el-input v-model="form.name" placeholder="请填写物品名称"></el-input>
      </el-form-item>
      <el-form-item label="物品类型">
        <el-select v-model="form.type" placeholder="请选择物品类型">
          <el-option label="日用品" value="日用品"></el-option>
          <el-option label="场地" value="场地"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物品描述">
        <el-input type="textarea" v-model="form.desc"></el-input>
      </el-form-item>
      <el-form-item label="租借价格">
        <el-col :span="11">
          <el-input type="num" placeholder="设置单次租借价格" v-model="form.price" style="width: 100%;"></el-input>
        </el-col>
        <el-col class="line" :span="2">/</el-col>
        <el-col :span="11">
          <el-select placeholder="设置单次租借时长" v-model="form.valuation" style="width: 100%;">
            <el-option label="日" value="日"></el-option>
            <el-option label="月" value="月"></el-option>
            <el-option label="年" value="年"></el-option>
          </el-select>
        </el-col>
      </el-form-item>

      <!-- <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      list-type="picture">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload> -->

      <!-- 上传商品图片 -->
      <el-upload
        class="avatar-uploader"
        action="https://jsonplaceholder.typicode.com/posts/"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form-item label="线下交货">
        <el-switch v-model="form.delivery"></el-switch>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit">上传物品</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-container>
</template>
  

<script>
  export default {
    data() {
      return {
        form: {
          name: '',
          type: '',
          desc: '',
          price: '',
          valuation: '',
          resource: '',
          delivery: false,
        },
        imageUrl: '',
        //fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      
      }
    },
    methods: {
      onSubmit() {
        console.log('submit!');
        let self = this
        // let file = e.target.files[0]
        /* eslint-disable no-undef */
        // 创建数据对象
        let param = {
          id:null,
          owner_id : localStorage.getItem("id"),
          name : this.form.name,
          type: this.form.type,
          borrow_state:null,
          check_state:null,
          description:this.form.desc,
          picture : this.imageUrl,
          price : this.form.price,
          valuation : this.form.valuation
        }
        
        console.log(this.imageUrl)
        console.log(this.form.desc)

        let config = {
          headers: {'Content-Type': 'application/json','Authorization':localStorage.getItem("token")}
        }

        // 添加请求头
        this.$axios.post('/item', param, config)
        .then(function (response) {
        // handle success
          console.log(response);
        })
        .catch(function (error) {
        // handle error
          console.log(error);
        })
        .then(function () {
        // always executed
          // console.log(this.param)
        });
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        console.log('图片路径1'+this.imageUrl)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        console.log('图片路径2'+file)
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
        },
        handleRemove(file, fileList) {
          console.log(file, fileList);
        },
        handlePreview(file) {
          console.log(file);
        }
    }
  }      
</script>

<style lang="less" scoped>
.release_container {
  height: 100%;
}

.avatar-uploader .el-upload {
    border: 2px dashed #130303;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>